package com.shs.sys.service.impl;

import com.shs.sys.entity.Role;
import com.shs.sys.mapper.RoleMapper;
import com.shs.sys.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jxm
 * @since 2020-04-14
 */
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Override
    public boolean removeById(Serializable id) {
        //根据角色id删除角色所拥有的权限sys_role_permission
        this.baseMapper.deleteRolePermissionById(id);
        //根据角色id删除用户所拥有的角色sys_role_user
        this.baseMapper.deleteRoleUserById(id);
        return super.removeById(id);
    }

    /**
     * 根据角色ID查询当前角色拥有的所有的权限或菜单ID
     * @param rid
     * @return
     */
    @Override
    public List<Integer> queryRolePermissionIdsByRid(Integer rid) {
        return this.baseMapper.queryRolePermissionIdsByRid(rid);
    }

    /**
     * 保存角色和菜单权限之间的关系
     * @param rid
     * @param ids
     */
    @Override
    public void saveRolePermission(Integer rid, Integer[] ids) {
        RoleMapper baseMapper = this.getBaseMapper();
        //根据rid删除sys_role_permission
        baseMapper.deleteRolePermissionById(rid);
        //保存
        if(null!=ids && ids.length>0){
            for (Integer id : ids) {
                baseMapper.saveRolePermission(rid,id);
            }
        }
    }

    /**
     * 根据当前用户id查询拥有的角色ID集合
     * @param uid
     */
    @Override
    public List<Integer> queryUserRoleIdsByUid(Integer uid) {
        return this.getBaseMapper().queryUserRoleIdsByUid(uid);
    }
}
